﻿using Newtonsoft.Json;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.Data;
using System.Security.Cryptography;
using System.Text;

namespace ImageManager.Common
{
    public static class Utils
    {
        public static string Serialize(object data)
        {
            return JsonConvert.SerializeObject(data);
        }
        public static T? Deserialize<T>(string str)
        {
            return JsonConvert.DeserializeObject<T>(str);
        }

        public static T Clone<T>(T data)
        {
            return Deserialize<T>(Serialize(data));
        }
        public static void Restore<T>(T data, T backup)
        {
            foreach (var p in typeof(T).GetProperties())
            {
                p.SetValue(data, p.GetValue(backup));
            }
        }
        public static string MakePassword()
        {
            const string seed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.!@#$%^&*()_+=";
            return RandomNumberGenerator.GetString(seed, 8);
        }
        public static string MakeRandom()
        {
            const string seed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            return RandomNumberGenerator.GetString(seed, 8);
        }
        public static string Md5(string str)
        {
            if (String.IsNullOrWhiteSpace(str)) return "";
            string pwd = "";
            MD5 md5 = MD5.Create();
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
            for (int i = 0; i < s.Length; i++)
            {
                pwd = pwd + s[i].ToString("X2");
            }
            return pwd.ToUpper();
        }
        static int counter = 10000;
        static Random random = new Random();
        static object locker = new object();
        public static string CreatePk()
        {
            lock (locker)
            {
                if (counter >= 100000) counter = 10000;
                else counter++;
            }
            int r = random.Next(0, 9);
            string dt = DateTime.Now.ToString("yyMMddHHmmssfff");
            return dt + counter.ToString().Substring(1, 4) + r;
        }
        public static Stream ToExcelStream(DataTable dt)
        {
            var workbook = new HSSFWorkbook();//一個sheet最多65536行
            var count = 0;
            for (double i = 0; i < Convert.ToDouble(dt.Rows.Count) / Convert.ToDouble(65534); i++)//每个Excel文件的一个页签只能存放65536行数据
            {
                var row_index = 0;
                workbook.CreateSheet("Sheet" + i);
                var sheet = workbook.GetSheet("Sheet" + i);
                IRow row;
                row = sheet.CreateRow(row_index);
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    row.CreateCell(j).SetCellValue(dt.Columns[j].Caption.ToString());
                }
                row = sheet.CreateRow(++row_index);
                for (int j = 0; j < (dt.Rows.Count - count > 65534 ? 65534 : dt.Rows.Count - count); j++)
                {
                    var r = dt.Rows[j + count];
                    for (int k = 0; k < dt.Columns.Count; k++)
                    {
                        row.CreateCell(k).SetCellValue(r[k].ToString());
                    }
                    row = sheet.CreateRow(++row_index);
                }
                count += row_index - 2;
            }
            Stream stream = new MemoryStream();
            workbook.Write(stream);
            stream.Flush();
            return stream;
        }
    }
}
